<?php
    include_once("db_connect.php");
?>

<?php
    include_once("menu.php");
?>

<?php
    (is_numeric($_GET['project']))? $pro = $_GET['project'] : $pro=0;
    $new2 = pg_exec('select pro_name from public.caller_project WHERE pro_id='.$pro);
    $t2 = pg_fetch_array($new2);
    echo "<h2>".$t2['pro_name']."</h2>";
?>

<h2>Stats</h2> 

<table border=1>
<tr>
 <td>project</td>
 <td>retry</td>
 <td>done</td>
 <td>&gt;18</td>
 <td>&gt;9, &lt;=18</td>
 <td>&lt;=9</td>
 <td>faled</td>
</tr>

<?php
    $count = pg_exec("select pr.pro_id as id, MAX(ph.pho_count) as retry, MAX(pr.pro_name) as name, COUNT(*) as cnt from public.caller_results re
		    JOIN public.caller_phones ph ON (ph.pho_phone=re.res_phone)
		    JOIN public.caller_project pr ON (pr.pro_id=ph.pro_id)
		    WHERE re.res_uniqueid = 'Failed'
		    GROUP BY pr.pro_id
		    ");
    while($t = pg_fetch_array($count)){
	$arr[$t['id']]['name']=$t['name'];
	$arr[$t['id']]['cnt']=$t['cnt'];
	$arr[$t['id']]['retry']=$t['retry'];
    }

    $count_done = pg_exec("select pr.pro_id as id, COUNT(*) as cnt from public.caller_results re
		    JOIN public.caller_phones ph ON (ph.pho_phone=re.res_phone)
		    JOIN public.caller_project pr ON (pr.pro_id=ph.pro_id)
		    WHERE re.res_uniqueid != 'Failed'
		    GROUP BY pr.pro_id
		    ");
    while($t = pg_fetch_array($count_done)){
	$arr[$t['id']]['cnt_done']=$t['cnt'];
    }
    
    $count_done = pg_exec("select pr.pro_id as id, COUNT(*) as cnt from public.caller_results re
		    JOIN public.caller_phones ph ON (ph.pho_phone=re.res_phone)
		    JOIN public.caller_project pr ON (pr.pro_id=ph.pro_id)
		    WHERE re.res_uniqueid != 'Failed'
			AND re.res_count>18
		    GROUP BY pr.pro_id
		    ");
    while($t = pg_fetch_array($count_done)){
	$arr[$t['id']]['cnt_done1']=$t['cnt'];
    }

    $count_done = pg_exec("select pr.pro_id as id, COUNT(*) as cnt from public.caller_results re
		    JOIN public.caller_phones ph ON (ph.pho_phone=re.res_phone)
		    JOIN public.caller_project pr ON (pr.pro_id=ph.pro_id)
		    WHERE re.res_uniqueid != 'Failed'
			AND re.res_count<=18
			AND re.res_count>9
		    GROUP BY pr.pro_id
		    ");
    while($t = pg_fetch_array($count_done)){
	$arr[$t['id']]['cnt_done2']=$t['cnt'];
    }
    
    foreach ($arr as $key=>$val) {
	echo "<tr>
	    <td>".$val['name']."</td>
	    <td>".$val['retry']."</td>
	    <td>".$val['cnt_done']."</td>
	    <td>".$val['cnt_done1']."</td>
	    <td>".$val['cnt_done2']."</td>
	    <td>".($val['cnt_done']-$val['cnt_done1']-$val['cnt_done2'])."</td>
	    <td>".$val['cnt']."</td>
	</tr>";
    }
    
?>

<?php
/*
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tstart = $mtime; 

    $new = pg_exec('select * from public.caller_phones LIMIT 10');
    $rows = pg_num_rows($new);
    for($i=0;$i<$rows;$i++){
	$rnd[$i]=$i;
    }
    for ($i=0;$i<10;$i++){
      $rows--;
      if($rows>=0) {
	$r = rand(0,$rows);
	$t = pg_fetch_array($new,$rnd[$r]);
	echo "<tr>";
	echo "<td>".$t['pho_phone']."</td>";
	echo "<td>".$t['pho_count']."</td>";
	echo "<td>".$t['pho_done']."</td>";
	echo "</tr>";
	$rnd[$r]=$rnd[$rows];
      }
    }

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$tend = $mtime;
$totaltime = ($tend - $tstart);
printf ($totaltime);     
*/
?>

</table>

<?php    
    include_once("db_disconnect.php");
?>